//
//  AddBankCardViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/25.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AddBankCardViewController.h"

@interface AddBankCardViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *kaleixingTF;
@property (weak, nonatomic) IBOutlet UITextField *kaihuhangTF;

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *cardIDTF;

@property (nonatomic, assign) BOOL isWait;

@end

@implementation AddBankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];

    self.isWait = NO;

    switch (self.type) {
        case EditBankCardType_Add:{
            
            
            
        } break;
        case EditBankCardType_Update:{
            
            [self loadDefaultData];
            
        } break;
        default:
            break;
    }
    
}

- (void)loadDefaultData {
    
    self.title = @"修改银行卡";
    
    self.phoneTF.delegate = self;
    
    self.nameTF.text = self.defaultModel.userName;
    self.phoneTF.text = self.defaultModel.userPhoneNum;
    
    self.kaihuhangTF.text = self.defaultModel.bankName;
    self.kaleixingTF.text = self.defaultModel.cardTypeName;
    self.cardIDTF.text = self.defaultModel.bankCardID;
    
}

- (void)initView {
    
    self.title = self.vcTitle;
}

- (void)request_updateBankCard {
    
    self.isWait = YES;

    NSDictionary *dic = @{}.mutableCopy;
    
//    [dic setValue:@"" forKey:@"bankCode"];
    [dic setValue:self.kaihuhangTF.text forKey:@"bankName"];
    [dic setValue:self.cardIDTF.text forKey:@"bankNumber"];
//    [dic setValue:@"" forKey:@"cardType"];
    [dic setValue:self.kaleixingTF.text forKey:@"cardTypeName"];
    [dic setValue:self.defaultModel.ID forKey:@"id"];
    
    [dic setValue:self.nameTF.text forKey:@"name"];
    [dic setValue:self.phoneTF.text forKey:@"phone"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_BANKCARD_UPDATE];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        self.isWait = NO;

        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        self.isWait = NO;

    }];
    
}

- (void)request_saveBankCard {
    
    self.isWait = YES;

    NSDictionary *dic = @{}.mutableCopy;
    
    NSString * bankName =self.kaihuhangTF.text;
    NSString * cardTypeName = self.kaleixingTF.text;
    NSString * name = self.nameTF.text;

//    [dic setValue:@"" forKey:@"bankCode"];
    [dic setValue:bankName forKey:@"bankName"];
    [dic setValue:self.cardIDTF.text forKey:@"bankNumber"];
//    [dic setValue:@"" forKey:@"cardType"];
    [dic setValue:cardTypeName forKey:@"cardTypeName"];
    [dic setValue:@"" forKey:@"id"];

    [dic setValue:name forKey:@"name"];
    [dic setValue:self.phoneTF.text forKey:@"phone"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_BANKCARD_ADD];
    
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        self.isWait = NO;
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        self.isWait = NO;

    }];
    
}

- (IBAction)saveBankCardInfo:(id)sender {
    
    if (!self.isWait) {
        [self saveAction];
    }
}

- (void)saveAction {
    
    if (!self.nameTF.text.length) {
        
        [self alert:@"请输入持卡人姓名"];
    } else {
        
        if (!self.phoneTF.text.length) {
            
            [self alert:@"请输入手机号"];
            
        } else {
            
            if (!self.cardIDTF.text.length) {
                
                [self alert:@"请输入银行卡号"];
                
            } else {
                
                if (!self.kaleixingTF.text.length) {
                    
                    [self alert:@"请输入卡类型"];
                } else {
                    
                    if (!self.kaihuhangTF.text.length) {
                        
                        [self alert:@"请请输入开户行"];
                    } else {
                        
                        switch (self.type) {
                            case EditBankCardType_Add:{
                                
                                [self request_saveBankCard];
                                
                            } break;
                            case EditBankCardType_Update:{
                                
                                [self request_updateBankCard];
                                
                            } break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneTF) {
        // 这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            
            return YES;
            
        }  else if (self.phoneTF.text.length >= 11) {
            
            self.phoneTF.text = [textField.text substringToIndex:11];
            
            [SVProgressHUD showInfoWithStatus:@"手机号为11为数字"];
            [SVProgressHUD dismissWithDelay:0.5];
            
            return NO;
        }
    }
    
    return YES;
}

@end
